﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TimKiem
{
    public partial class frmNhapKho : Form
    {
        #region khởi tạo
        bool checkClose = false;
        bool check = false;
        public static Entities.HoaDonNhap hoadonnhap;
        Entities.HoaDonNhap[] hienthi;
        public frmNhapKho(string manhacungcap, string makho)
        {
            InitializeComponent();
            dgvHienThi.DataSource = HienThi(manhacungcap, makho);
            fix();
            hienthi = (Entities.HoaDonNhap[])dgvHienThi.DataSource;
        }

        public frmNhapKho(string makho)
        {
            InitializeComponent();
            dgvHienThi.DataSource = HienThi(makho);
            fix();
            hienthi = (Entities.HoaDonNhap[])dgvHienThi.DataSource;
        }

        public Entities.HoaDonNhap[] HienThi(string manhacungcap, string makho)
        {
            List<Entities.HoaDonNhap> array = new List<Entities.HoaDonNhap>();
            Entities.HoaDonNhap t = new Entities.HoaDonNhap();
            t.HanhDong = "1";
            t.MaHoaDonNhap = "";
            Entities.HoaDonNhap[] temp = Common.StaticVariable.InterfaceObject.Select_HoaDonNhap(t);
            Entities.ChiTietHoaDonNhap ct = new Entities.ChiTietHoaDonNhap();
            ct.HanhDong = "1";
            ct.MaHoaDonNhap = "";
            ct.MaHangHoa = "";
            Entities.ChiTietHoaDonNhap[] templv = Common.StaticVariable.InterfaceObject.Select_ChiTietHoaDonNhap(ct);
            foreach (Entities.HoaDonNhap item in temp)
            {
                if (item.Deleted == false)
                {

                    bool kt = false;
                    foreach (Entities.ChiTietHoaDonNhap item1 in templv)
                    {
                        if (item1.MaHoaDonNhap == item.MaHoaDonNhap && item.MaKho == makho && item.MaNhaCungCap == manhacungcap)
                        {
                            if (item1.SoLuongConLai != 0)
                            {
                                kt = true;
                                break;
                            }
                        }
                    }
                    if (kt)
                        array.Add(item);
                }
            }
            Entities.HoaDonNhap[] hoadonnhap = (Entities.HoaDonNhap[])array.ToArray();
            return hoadonnhap;
        }

        public Entities.HoaDonNhap[] HienThi(string makho)
        {
            List<Entities.HoaDonNhap> array = new List<Entities.HoaDonNhap>();
            Entities.HoaDonNhap t = new Entities.HoaDonNhap();
            t.HanhDong = "1";
            t.MaHoaDonNhap = "";
            Entities.HoaDonNhap[] temp = Common.StaticVariable.InterfaceObject.Select_HoaDonNhap(t);
            Entities.ChiTietHoaDonNhap ct = new Entities.ChiTietHoaDonNhap();
            ct.HanhDong = "4";
            ct.MaHoaDonNhap = "";
            ct.MaHangHoa = "";
            Entities.ChiTietHoaDonNhap[] templv = Common.StaticVariable.InterfaceObject.Select_ChiTietHoaDonNhap(ct);
            foreach (Entities.HoaDonNhap item in temp)
            {
                if (item.Deleted == false)
                {
                    bool kt = false;
                    foreach (Entities.ChiTietHoaDonNhap item1 in templv)
                    {
                        if (item1.MaHoaDonNhap == item.MaHoaDonNhap && item.MaKho == makho)
                        {
                            if (item1.SoLuongConLai != 0)
                            {
                                kt = true;
                                break;
                            }
                        }
                    }
                    if (kt)
                        array.Add(item);
                }
            }
            Entities.HoaDonNhap[] hoadonnhap = (Entities.HoaDonNhap[])array.ToArray();
            return hoadonnhap;
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.HoaDonNhap.MaHoaDonNhap].Visible = true;
            dgvHienThi.Columns[Common.Constants.HoaDonNhap.NgayNhap].Visible = true;
            dgvHienThi.Columns[Common.Constants.HoaDonNhap.TenNhaCungCap].Visible = true;

            dgvHienThi.Columns[Common.Constants.HoaDonNhap.MaHoaDonNhap].HeaderText = "Mã Hóa Đơn";
            dgvHienThi.Columns[Common.Constants.HoaDonNhap.NgayNhap].HeaderText = "Ngày Nhập";
            dgvHienThi.Columns[Common.Constants.HoaDonNhap.TenNhaCungCap].HeaderText = "Tên Nhà Cung Cấp";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Tìm Kiếm
        public void TimKiem()
        {
            dgvHienThi.DataSource = (Entities.HoaDonNhap[])hienthi.Where(p => p.MaHoaDonNhap.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
        }
        #endregion

        #region Chọn
        void Chon()
        {
            try
            {
                if (i < 0)
                    return;
                if (dgvHienThi.RowCount == 0)
                    return;
                hoadonnhap = ((Entities.HoaDonNhap[])dgvHienThi.DataSource)[i];
                if (hoadonnhap == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                checkClose = true;
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Event

        private void frmNhapKho_Load(object sender, EventArgs e)
        {
            btnChon.Click += new EventHandler(btnChon_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            btnTim.Click += new EventHandler(btnTim_Click);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
            this.FormClosing += new FormClosingEventHandler(frmKhachHang_FormClosing);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Chon();
            }
        }

        void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        void frmKhachHang_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Chon();
        }
        int i;
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnChon_Click(object sender, EventArgs e)
        {
            Chon();
        }
        #endregion

    }
}
